package com.ruoyi.system.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.generator.exception.BizException;
import com.ruoyi.generator.util.BeanCopyUtils;
import com.ruoyi.generator.util.CommonUtils;
import com.ruoyi.generator.util.UserUtils;
import com.ruoyi.system.domain.Talk;
import com.ruoyi.system.domain.dto.TalkBackDTO;
import com.ruoyi.system.domain.dto.TalkDTO;
import com.ruoyi.system.domain.vo.TalkVo;
import com.ruoyi.system.mapper.CommentMapper;
import com.ruoyi.system.mapper.TalkMapper;
import com.ruoyi.system.service.RedisService;
import com.ruoyi.system.service.TalkService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static com.ruoyi.common.enums.RedisPrefixConst.TALK_LIKE_COUNT;
import static com.ruoyi.common.enums.RedisPrefixConst.TALK_USER_LIKE;

@Service
public class TalkServiceImpl implements TalkService {

    @Autowired
    private TalkMapper talkDao;

    @Autowired
    private CommentMapper commentDao;

    @Autowired
    private RedisService redisService;

    @Override
    public List<TalkDTO> getAllTalks(Long userid) {
        List<TalkDTO> allTalks = talkDao.getAllTalks(userid);
        for(TalkDTO talkDTO : allTalks) {
            Boolean aBoolean = redisService.sIsMember(userid.toString(), talkDTO.getId());
            talkDTO.setLikes(aBoolean);
            if(redisService.hasKey("121"+talkDTO.getId().toString()+"1")){
                Object o = redisService.get("121"+talkDTO.getId().toString()+"1");
                talkDTO.setLikesCount(Integer.valueOf(o.toString()).longValue());
            }
            else{
                redisService.set("121"+talkDTO.getId().toString()+"1",0);
                talkDTO.setLikesCount(0l);
            }
        }
        return allTalks;
    }
    @Override
    public List<TalkDTO> getTalkByUserId(Long userId) {
        List<TalkDTO> talkByUserId = talkDao.getTalkByUserId(userId);
        for (TalkDTO talkDTO : talkByUserId) {
            Boolean aBoolean = redisService.sIsMember(userId.toString(), talkDTO.getId());
            talkDTO.setLikes(aBoolean);
            if (redisService.hasKey("121"+talkDTO.getId().toString()+"1")) {
                Object o = redisService.get("121"+talkDTO.getId().toString()+"1");
                talkDTO.setLikesCount(Integer.valueOf(o.toString()).longValue());
            } else {
                redisService.set("121"+talkDTO.getId().toString()+"1", 0);
                talkDTO.setLikesCount(0l);
            }
        }
        return talkByUserId;
    }

    @Override
    public TalkDTO getTalkById(Long userId,Long talkId) {
        TalkDTO talkDTO = talkDao.getTalkById(userId,talkId);
        if(Objects.isNull(talkDTO)){
            throw new BizException("说说不存在");
        }
        else{
            Boolean aBoolean = redisService.sIsMember(userId.toString(), talkDTO.getId());
            talkDTO.setLikes(aBoolean);
            if (redisService.hasKey("121"+talkDTO.getId().toString()+"1")) {
                Object o = redisService.get("121"+talkDTO.getId().toString()+"1");
                talkDTO.setLikesCount(Integer.valueOf(o.toString()).longValue());
            } else {
                redisService.set("121"+talkDTO.getId().toString()+"1", 0);
                talkDTO.setLikesCount(0l);
            }
        }
//        if(Objects.nonNull(talkDTO.getImages())){
//            talkDTO.setImgList(CommonUtils.castList(JSON.parseObject(talkDTO.getImages(),List.class),String.class));
//        }
        talkDao.updateViews(talkId);
        return talkDTO;
    }

    @Override
    public List<TalkBackDTO> listBackTalks() {
        return talkDao.listBackTalks();
    }

    @Override
    public TalkBackDTO getBackTalkById(Long userid,Long talkId) {
        TalkBackDTO talkBackDTO = talkDao.getBackTalkById(userid,talkId);
        // 转换图片格式
        if (Objects.nonNull(talkBackDTO.getImages())) {
            talkBackDTO.setImgList(CommonUtils.castList(com.alibaba.fastjson.JSON.parseObject(talkBackDTO.getImages(), List.class), String.class));
        }
        return talkBackDTO;
    }


    @Override
    public Integer getViewTotal() {
        return talkDao.getViewTotal();
    }

    @Override
    public int saveTalk(Talk talk) {
        return talkDao.saveTalk(talk);
    }

    @Override
    public int updateTalk(Talk talk) {
        return talkDao.updateTalk(talk);
    }

    @Override
    public void deleteTalk(Long id) {
        talkDao.deleteTalk(id);
    }

    @Override
    public int saveTalkLike(Long userid,Long talkId){
        TalkDTO talkById = talkDao.getTalkById(userid,talkId);
        int flag = 0;
        if(redisService.sIsMember(userid.toString(),talkId)){
            redisService.sRemove(userid.toString(),talkId);
            redisService.decr("121"+talkId.toString()+"1",1);
            return flag;
        }
        if(!redisService.sIsMember(userid.toString(),talkId)){
            flag = 1;
            redisService.sAdd(userid.toString(),talkId);
            redisService.incr("121"+talkId.toString()+"1",1);
            return flag;
        }
        return flag;
    }

}


